﻿using UnityEngine;
using System.Collections;

public class RTPawn : MonoBehaviour {

	public enum EAirState
	{
		None,
		UpAir,
		Finishing,
		Rising,
	}
	EAirState curState;
	int nState;

	public float upLimit=5;
	public float upStartForce=6;
	public ForceMode upStartForceMode = ForceMode.VelocityChange;
	public float upDamageForce=3;
	public ForceMode upDamageForceMode = ForceMode.VelocityChange;

	public float downForce=-24;
	public ForceMode downForceMode = ForceMode.VelocityChange;

	Vector3 newPos;
	Animation anim;
	new Transform transform;
	bool isGrounded;
	bool isFinishAtk;
	bool isPlayingDown;
	public UILabel hitLbl;
	int hitCount;

	void Awake()
	{
		isGrounded=true;
		anim = GetComponent<Animation>();
		transform = gameObject.transform;
		rigidbody.useGravity=false;
		rigidbody.isKinematic=true;
	}

	void IncHit()
	{
//		hitLbl.gameObject.SetActive (true);
//		hitCount++;
//		if(hitCount==1)
//			hitLbl.text = string.Format("{0} HIT",hitCount);
//		else
//			hitLbl.text = string.Format("{0} HITS",hitCount);
	}

	void Update()
	{
		if(Input.GetKeyDown(KeyCode.Q) ) {

			IncHit();
			isPlayingDown=false;
			isGrounded=false;
		
			nState=1;
			anim.Play("airborne_up");
			rigidbody.useGravity=true;
			rigidbody.isKinematic=false;
			rigidbody.velocity = Vector3.zero;
			rigidbody.AddForce(new Vector3(0,upStartForce,0),upStartForceMode);

		}
		if(Input.GetKeyDown(KeyCode.W) ) {
			if(nState==1 && !isFinishAtk) {
				IncHit();
				isPlayingDown=false;
				anim.Play("airborne_hit");
				rigidbody.useGravity=true;
				rigidbody.isKinematic=false;
				rigidbody.velocity = Vector3.zero;
				rigidbody.AddForce(new Vector3(0,upDamageForce,0),upDamageForceMode);
			}
		}
		if(Input.GetKeyDown(KeyCode.E) ) {
			if(nState==1) {
				IncHit();
				isPlayingDown=true;
				anim.Play("airborne_finish");
				nState=10;
				isFinishAtk=true;
				rigidbody.velocity = Vector3.zero;
				rigidbody.useGravity=true;
				rigidbody.isKinematic=false;
				rigidbody.AddForce(new Vector3(0,downForce,0),downForceMode);
			}
		}
		
		if(transform.position.y<0) {
			hitCount=0;
			isPlayingDown=false;
			isGrounded=true;
			nState=0;
			if(isFinishAtk) {
				anim.Play("airborne_finish_end");
			}
			else
				anim.Play("airborne_end");
			anim.PlayQueued("raise");
			isFinishAtk=false;
			rigidbody.useGravity=false;
			rigidbody.isKinematic=true;
			newPos = transform.position;
			newPos.y = 0;
			transform.position = newPos;
		}
		//Debug.Log(rigidbody.velocity.y);
		if(!isGrounded && rigidbody.velocity.y<0)
		{
			if(!isPlayingDown) {
				isPlayingDown=true;
				anim.CrossFadeQueued("airborne_down");
			}
		}

	}

//	IEnumerator PlayWithOptions(string animName, Action onComplete)
//	{
//		anim.Play(animName);
//		
//		while (anim.isPlaying) { yield return null; }
//		
//		onComplete();
//	}

	void FixedUpdate()
	{
		if(transform.position.y>upLimit) {
			newPos = transform.position;
			newPos.y = upLimit;
			transform.position = newPos;
			rigidbody.position = newPos;
			rigidbody.velocity=Vector3.zero;
		}

	}
}
